using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Pavilion;

namespace Pavilion.Control
{
    public class DropDownItem : Control
    {
        private DropDownMenu dropDownMenu;
        private object associatedObject;
        private string text;
        private bool fillItem;

        public event Action<DropDownItem> ItemPressed;

        public DropDownItem(string text) : base()
        {
            dropDownMenu = new DropDownMenu();
            Text = text;
            Initialization();
        }

        public DropDownItem(string text, object associatedObject) : base()
        {
            dropDownMenu = new DropDownMenu();
            Text = text;
            AssociatedObject = associatedObject;
            Initialization();
        }

        private void Initialization()
        {
            BackgroundColor = Color.DarkGray;

            this.Input.Enter += new EventHandler(DropDownItem_Enter);
            this.Input.Leave += new EventHandler(DropDownItem_Leave);
            this.Input.MouseRelease += new MouseObjectHandler(DropDownItem_MouseRelease);
        }

        public override void Draw()
        {
            base.Draw();

            if (fillItem)
            {
                DrawManager.Instance.FillRectangleOnScreen(ScreenBounds, BackgroundColor);
                DrawManager.Instance.DrawTextOnScreen(text, new Vector2(ScreenBounds.Left + 5, ScreenBounds.Top), Color.White, 0.3f, false);
            }
            else
            {
                DrawManager.Instance.DrawTextOnScreen(text, new Vector2(ScreenBounds.Left + 5, ScreenBounds.Top), Color.Black, 0.25f, false);
            }
        }

        /// <summary>
        /// Adds a DropDownItem to this control.
        /// </summary>
        public void AddItem(DropDownItem item)
        {
            dropDownMenu.AddItem(item);
        }

        /// <summary>
        /// Adds a range of items to this menu.
        /// </summary>
        /// <param name="dropDownItems"></param>
        public void AddRange(DropDownItem[] dropDownItems)
        {
            for (int i = 0; i < dropDownItems.Length; i++)
                AddItem(dropDownItems[i]);
        }

        /// <summary>
        /// Removes the item form the list.
        /// </summary>
        /// <param name="item">The item to be removed.</param>
        public bool Remove(DropDownItem item)
        {
            if (dropDownMenu.Remove(item))
                return true;

            return false;
        }

        /// <summary>
        /// Removes the list of items in the menu of this item.
        /// </summary>
        public void Clear()
        {
            dropDownMenu.Clear();
        }

        /// <summary>
        /// Gets the amount of items that are in this DropDownItem.
        /// </summary>
        public int Count
        {
            get { return dropDownMenu.Count; }
        }

        /// <summary>
        /// Gets or sets the value at the given index.
        /// </summary>
        /// <param name="index">The index of the item within this list.</param>
        public DropDownItem this[int index]
        {
            get { return dropDownMenu[index]; }
            set
            {
                Remove(dropDownMenu[index]);
                AddItem(value);
            }
        }

        /// <summary>
        /// Gets or sets the text value of this item.
        /// </summary>
        public string Text
        {
            get { return text; }
            set { text = value; }
        }

        /// <summary>
        /// Gets or sets the object that is associated with this item.
        /// </summary>
        public object AssociatedObject
        {
            get { return associatedObject; }
            set { associatedObject = value; }
        }

        private void DropDownItem_Leave(object sender, EventArgs e)
        {
            fillItem = false;
        }

        private void DropDownItem_Enter(object sender, EventArgs e)
        {
            fillItem = true;
        }

        private void DropDownItem_MouseRelease(object sender, MouseCursor mouseCursor)
        {
            if (ItemPressed != null)
                ItemPressed(this);

            if (Count > 0)
            {
                dropDownMenu.Visible = true;
                RootControl.Instance.Collection.Add(dropDownMenu);
                dropDownMenu.Position = new Point(Parent.ScreenBounds.Width + Parent.ScreenBounds.X, ScreenBounds.Y);
            }
        }
    }
}
